package com.example.capturetheflag.helper;

import java.util.Iterator;
import java.util.LinkedList;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.example.capturetheflag.connection.HttpGetRequest;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class GameFieldSimple extends Overlay {
	LinkedList<GeoPoint> fieldPoints = new LinkedList<GeoPoint>();
	Path gameBorder;
	protected int pointsNum;
	private Paint paint;

	public GameFieldSimple() {
		super();
		gameBorder = new Path();
		paint = new Paint();
		paint.setARGB(255, 255, 0, 0);
		paint.setStrokeWidth(3);
		paint.setStyle(Paint.Style.STROKE);
		JSONObject jo;
		try {
			jo = Singleton.getInstance().gameField
					.getJSONObject("GetGameAreaResult");
			if (jo.equals(null)) {
				HttpGetRequest httpGetRequest = new HttpGetRequest();
				String url = "http://proinz.studentlive.pl/GeoService.svc/GetGameArea/"
						+ Singleton.getInstance().gameID;
				httpGetRequest.execute(url);
			}
		} catch (Exception e) {

		}
		try {
			jo = Singleton.getInstance().gameField
					.getJSONObject("GetGameAreaResult");
			String ax = jo.get("latitude_area_a").toString();
			String ay = jo.get("longitude_area_a").toString();
			String bx = jo.get("latitude_area_b").toString();
			String by = jo.get("longitude_area_b").toString();
			String cx = jo.get("latitude_area_c").toString();
			String cy = jo.get("longitude_area_c").toString();
			String dx = jo.get("latitude_area_d").toString();
			String dy = jo.get("longitude_area_d").toString();
			GeoPoint a = new GeoPoint(Integer.decode(ax), Integer.decode(ay));
			GeoPoint b = new GeoPoint(Integer.decode(bx), Integer.decode(by));
			GeoPoint c = new GeoPoint(Integer.decode(cx), Integer.decode(cy));
			GeoPoint d = new GeoPoint(Integer.decode(dx), Integer.decode(dy));
			AddGeoPoint(a);
			AddGeoPoint(b);
			AddGeoPoint(c);
			AddGeoPoint(d);
			Singleton.getInstance().a = a;
			Singleton.getInstance().c = c;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void AddGeoPoint(GeoPoint gp) {
		// if(fieldPoints.size()>=pointsNum)
		// {
		// while(fieldPoints.size()>0) fieldPoints.removeFirst();
		// gameBorder.reset();
		// }
		fieldPoints.addLast(gp);
	}

	public GeoPoint getCenterOfField() {
		int x1 = 0, x2 = 0, y1 = 0, y2 = 0, i = 0;
		for (Iterator<GeoPoint> it = fieldPoints.iterator(); it.hasNext();) {
			GeoPoint gp = (GeoPoint) it.next();
			if (i == 0) {
				x1 = x2 = gp.getLatitudeE6();
				y1 = y2 = gp.getLongitudeE6();
			} else {
				if (gp.getLatitudeE6() < x1)
					x1 = gp.getLatitudeE6();
				if (gp.getLatitudeE6() > x2)
					x2 = gp.getLatitudeE6();
				if (gp.getLongitudeE6() < y1)
					y1 = gp.getLongitudeE6();
				if (gp.getLongitudeE6() > y2)
					y2 = gp.getLongitudeE6();
			}
			i++;
		}
		int X, Y;
		X = (x2 + x1) / 2;
		Y = (y2 + y1) / 2;
		return new GeoPoint(X, Y);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		gameBorder.reset();

		Projection pr = mapView.getProjection();

		int rozm = fieldPoints.size();
		int x = 0, y = 0;

		Point pt = new Point();
		if (rozm < 2)
			return;
		int i = 0;

		for (Iterator<GeoPoint> it = fieldPoints.iterator(); it.hasNext();) {
			GeoPoint gp = (GeoPoint) it.next();
			pr.toPixels(gp, pt);

			if (i == 0) {
				gameBorder.moveTo(pt.x, pt.y);
				x = pt.x;
				y = pt.y;
			} else {
				gameBorder.lineTo(pt.x, pt.y);
				if (i == 3)
					gameBorder.lineTo(x, y);
			}
			i++;
		}
		canvas.drawPath(gameBorder, paint);
	}
}
